// 
//   FILE:  dht11_test1.pde
// PURPOSE: DHT11 library test sketch for Arduino
//

//Celsius to Fahrenheit conversion
double Fahrenheit(double celsius)
{
  return 1.8 * celsius + 32;
}

// fast integer version with rounding
//int Celcius2Fahrenheit(int celcius)
//{
//  return (celsius * 18 + 5)/10 + 32;
//}


//Celsius to Kelvin conversion
double Kelvin(double celsius)
{
  return celsius + 273.15;
}

// dewPoint function NOAA
// reference (1) : http://wahiduddin.net/calc/density_algorithms.htm
// reference (2) : http://www.colorado.edu/geography/weather_station/Geog_site/about.htm
//
double dewPoint(double celsius, double humidity)
{
  // (1) Saturation Vapor Pressure = ESGG(T)
  double RATIO = 373.15 / (273.15 + celsius);
  double RHS = -7.90298 * (RATIO - 1);
  RHS += 5.02808 * log10(RATIO);
  RHS += -1.3816e-7 * (pow(10, (11.344 * (1 - 1/RATIO ))) - 1) ;
  RHS += 8.1328e-3 * (pow(10, (-3.49149 * (RATIO - 1))) - 1) ;
  RHS += log10(1013.246);

  // factor -3 is to adjust units - Vapor Pressure SVP * humidity
  double VP = pow(10, RHS - 3) * humidity;

  // (2) DEWPOINT = F(Vapor Pressure)
  double T = log(VP/0.61078);   // temp var
  return (241.88 * T) / (17.558 - T);
}

// delta max = 0.6544 wrt dewPoint()
// 6.9 x faster than dewPoint()
// reference: http://en.wikipedia.org/wiki/Dew_point
double dewPointFast(double celsius, double humidity)
{
  double a = 17.271;
  double b = 237.7;
  double temp = (a * celsius) / (b + celsius) + log(humidity*0.01);
  double Td = (b * temp) / (a - temp);
  return Td;
}


#include <dht11.h>
#include <InvertedTM1638.h>
#include <TM1638.h>
#include <TM1640.h>
#include <TM16XX.h>
#include <TM16XXFonts.h>

#define VENTO 14
#define DHT11PIN 2
dht11 DHT11;

//#define DISPLAYLEDPIN 11
//#define DISPLAYLEDPIN 11
//#define DISPLAYLEDPIN 11
TM1638 DisplayLED(8, 9, 7);
long value = 0x1234ABCD;
byte dots = 1;
unsigned long a,brightness=2;

struct Clima {
  long humidade;
  long temperatura;
};

//SOUND MONITOR for the classroom 
int sensorAOPin = A0;    //Microphone Sensor Analog Output Pin
int sensorDOPin = 4;    //Microphone Sensor Digital Output Pin   
int sensorDOValue = 0;    
int sensorValue = 0; 

void setup()
{
  Serial.begin(9600);
  DisplayLED.setupDisplay(true, brightness); // where 7 is intensity (from 0~7)
  digitalWrite(7, HIGH); //We need to set it HIGH immediately on boot
  pinMode(7,OUTPUT);  
}

void loop()
{
  Clima variavel = { 0,0 };
  Serial.println("\n");

  int chk = DHT11.read(DHT11PIN);

  Serial.println("Read sensor: ");
  switch (chk)
  {
  case DHTLIB_OK: 
    Serial.println("OK"); 
    variavel.temperatura = DHT11.temperature;
    variavel.humidade = DHT11.humidity;
    break;
  case DHTLIB_ERROR_CHECKSUM: 
    Serial.println("Checksum error"); 
    break;
  case DHTLIB_ERROR_TIMEOUT: 
    Serial.println("Time out error"); 
    break;
  default: 
    Serial.println("Unknown error"); 
    break;
  }

  // light the first 4 red LEDs and the last 4 green LEDs as the buttons are pressed
  byte keys = DisplayLED.getButtons();
  //  DisplayLED.clearDisplay();
  switch(keys)
  {
  case 1: //Botao1
    DisplayLED.setLED(TM1638_COLOR_GREEN, 0);
    Serial.println("Botao1");
    Serial.println("Humidity (%): ");
    Serial.println((float)variavel.humidade, 2);
    DisplayLED.setDisplayToDecNumber((float)variavel.humidade,0,false);
    DisplayLED.setDisplayToString("HUM",7 ,false);
    break;
  case 2: //Botao2
    DisplayLED.setLED(TM1638_COLOR_GREEN, 1);
    Serial.println("Botao2");
    Serial.println("Sensacao Termica (°C): ");
    Serial.println((float)variavel.temperatura, 2);
    long st;
    st = 33 + (10*sqrt(VENTO)+10.45-VENTO) * (variavel.temperatura-33)/22;
    Serial.println((float)st, 2);
    DisplayLED.setDisplayToDecNumber((float)variavel.temperatura,0,false);
    DisplayLED.setDisplayToString("St",7,false);
    break;
  case 3: //Botao1 e Botao2
    DisplayLED.setLED(TM1638_COLOR_GREEN, 0);
    DisplayLED.setLED(TM1638_COLOR_GREEN, 1);
    Serial.println("Botao1 e Botao2");
    for (a=10000; a<110000; a++)
    {
      DisplayLED.setDisplayToDecNumber(a,4,false);
      delay(1);
    }
    break;
  case 4: //Botao3
    DisplayLED.setLED(TM1638_COLOR_GREEN, 2);
    Serial.println("Botao4");
    Serial.println("Temperature (°F): ");
    Serial.println(Fahrenheit(variavel.temperatura), 2);
    DisplayLED.setDisplayToDecNumber((float)Fahrenheit(variavel.temperatura),0,false);
    DisplayLED.setDisplayToString("FAr",7 ,false);
    break;
  case 8: //Botao4
    DisplayLED.setLED(TM1638_COLOR_GREEN, 3);
    Serial.println("Botao4");
    Serial.println("Temperature (°K): ");
    Serial.println(Kelvin(DHT11.temperature), 2);
    DisplayLED.setDisplayToDecNumber((float)Kelvin(variavel.temperatura),0,false);
    DisplayLED.setDisplayToString("KEL",7 ,false);
    break;
  case 16: //Botao5
    DisplayLED.setLED(TM1638_COLOR_GREEN, 4);
    Serial.println("Botao5");
    Serial.println("Dew Point (°C): ");
    Serial.println(dewPoint(DHT11.temperature, variavel.humidade));
    DisplayLED.setDisplayToDecNumber((float)dewPoint(variavel.temperatura, variavel.humidade),0,false);
    DisplayLED.setDisplayToString("dEPt",7 ,false);
    break;
  case 32: //Botao6
    DisplayLED.setLED(TM1638_COLOR_GREEN, 5);
    Serial.println("Botao6");
    Serial.println("Dew PointFast (°C): ");
    Serial.println(dewPointFast(variavel.temperatura, variavel.humidade));
    DisplayLED.setDisplayToDecNumber((float)dewPointFast(DHT11.temperature, variavel.humidade),0,false);
    DisplayLED.setDisplayToString("dEPtF",7 ,false);
    break;
  case 64: //Botao7
    DisplayLED.clearDisplay();
    DisplayLED.setLED(TM1638_COLOR_GREEN, 6);
    Serial.println("Botao7, limpar LCD");
    Serial.println("Humidity (%): ");
    Serial.println((float)variavel.humidade, 2);
    Serial.println("Temperature (°C): ");
    Serial.println((float)variavel.temperatura, 2);
    Serial.println("Temperature (°F): ");
    Serial.println(Fahrenheit(variavel.temperatura), 2);
    Serial.println("Temperature (°K): ");
    Serial.println(Kelvin(variavel.temperatura), 2);
    Serial.println("Dew Point (°C): ");
    Serial.println(dewPoint(variavel.temperatura, variavel.humidade));
    Serial.println("Dew PointFast (°C): ");
    Serial.println(dewPointFast(variavel.temperatura, variavel.humidade));
    break;
  case 128: //Botao8
    DisplayLED.setLED(TM1638_COLOR_GREEN, 7);
    Serial.println("Botao8");
    if(brightness<=0)
    {
      brightness = 7;
      Serial.println("Brightness reset to"+brightness);
    }
    else
    {
      DisplayLED.setupDisplay(true, brightness); // where 7 is intensity (from 0~7)
      DisplayLED.setDisplayToString("bright"+(String)brightness, dots, true);
      brightness--;
    }
    break;
  default:
    DisplayLED.setDisplayToString("t"+(String)variavel.temperatura+"oC", dots, true);
    DisplayLED.setLEDs(0xFFFF);
    break;
  }

  Serial.println("Brightness: "+(String)brightness);
  Serial.println("Key pressed: "+(String)keys);

  //  DisplayLED.setLEDs(((keys & 0xF0) << 8) | (keys & 0xF));
  //  DisplayLED.setLED(TM1638_COLOR_RED, 1);  // set LED number x to red
  //  DisplayLED.setLED(TM1638_COLOR_GREEN, 2); // set LED number x to green
  //  DisplayLED.setLED(TM1638_COLOR_GREEN, 3); // set LED number x to red and green
  //  DisplayLED.setLEDs(0xE007);
  //  DisplayLED.setupDisplay(true, 7); // where 7 is intensity (from 0~7)
  //  DisplayLED.setDisplayToDecNumber(10,0,false);

  sensorValue = analogRead(sensorAOPin);    
  Serial.print("SensorAnalogValue:");
  Serial.println(sensorValue);
  DisplayLED.setDisplayToDecNumber(float(sensorValue),0,false);
//  DisplayLED.setLEDs(sensorValue);
  sensorDOValue = digitalRead(sensorDOPin);
  Serial.print("SensorDigitalValue:");
  Serial.println(sensorDOValue);

  //  for (a=10000; a<11000; a++)
  //  {
  //    DisplayLED.setDisplayToDecNumber(a,0,true);
  //    delay(1);
  //  }
  //  byte valores[] = { 
  //    1, 2, 4, 8, 16, 32, 64, 128         };
  //  DisplayLED.setDisplay(valores);
  //  delay(1000);
  //  byte valores2[] = { 
  //    0xa,07,06,05,04,03,02,01   };
  //  DisplayLED.setDisplay(valores2);
  //
  //  DisplayLED.setDisplayToDecNumber(keys,0,false);
  //  delay(1000);

  dots = (dots * 2);
  if (dots == 0) {
    dots = 1;
  }
  delay(500);
}










